#!/bin/bash
#
# Copyright (C) 2007 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Set up prog to be the path of this script, including following symlinks,
# and set up progdir to be the fully-qualified pathname of its directory.
prog="$0"
while [ -h "${prog}" ]; do
    newProg=`/bin/ls -ld "${prog}"`
    newProg=`expr "${newProg}" : ".* -> \(.*\)$"`
    if expr "x${newProg}" : 'x/' >/dev/null; then
        prog="${newProg}"
    else
        progdir=`dirname "${prog}"`
        prog="${progdir}/${newProg}"
    fi
done
oldwd=`pwd`
progdir=`dirname "${prog}"`
cd "${progdir}"
progdir=`pwd`
prog="${progdir}"/`basename "${prog}"`
test_dir="test-$$"
if [ -z "$TMPDIR" ]; then
  tmp_dir="/tmp/$USER/${test_dir}"
else
  tmp_dir="${TMPDIR}/$USER/${test_dir}"
fi

export JAVA="java"
export JAVAC="javac -g"
export RUN="${progdir}/etc/push-and-run-test-jar"
export DEX_LOCATION=/data/run-test/${test_dir}
export NEED_DEX="true"

# If dx was not set by the environment variable, assume it is in the path.
if [ -z "$DX" ]; then
  export DX="dx"
fi

# If jasmin was not set by the environment variable, assume it is in the path.
if [ -z "$JASMIN" ]; then
  export JASMIN="jasmin"
fi


info="info.txt"
build="build"
run="run"
expected="expected.txt"
check_cmd="check"
output="output.txt"
build_output="build-output.txt"
lib="libartd.so"
run_args="--quiet"

prebuild_mode="yes"
target_mode="yes"
dev_mode="no"
update_mode="no"
debug_mode="no"
relocate="yes"
runtime="art"
usage="no"
build_only="no"
suffix64=""
trace="false"
always_clean="no"

while true; do
    if [ "x$1" = "x--host" ]; then
        target_mode="no"
        DEX_LOCATION=$tmp_dir
        shift
    elif [ "x$1" = "x--jvm" ]; then
        target_mode="no"
        runtime="jvm"
        NEED_DEX="false"
        shift
    elif [ "x$1" = "x-O" ]; then
        lib="libart.so"
        shift
    elif [ "x$1" = "x--dalvik" ]; then
        lib="libdvm.so"
        runtime="dalvik"
        shift
    elif [ "x$1" = "x--relocate" ]; then
        relocate="yes"
        shift
    elif [ "x$1" = "x--no-relocate" ]; then
        relocate="no"
        shift
    elif [ "x$1" = "x--prebuild" ]; then
        prebuild_mode="yes"
        shift;
    elif [ "x$1" = "x--no-prebuild" ]; then
        prebuild_mode="no"
        shift;
    elif [ "x$1" = "x--image" ]; then
        shift
        image="$1"
        run_args="${run_args} --image $image"
        shift
    elif [ "x$1" = "x-Xcompiler-option" ]; then
        shift
        option="$1"
        run_args="${run_args} -Xcompiler-option $option"
        shift
    elif [ "x$1" = "x--runtime-option" ]; then
        shift
        option="$1"
        run_args="${run_args} --runtime-option $option"
        shift
    elif [ "x$1" = "x--debug" ]; then
        run_args="${run_args} --debug"
        shift
    elif [ "x$1" = "x--gdb" ]; then
        run_args="${run_args} --gdb"
        dev_mode="yes"
        shift
    elif [ "x$1" = "x--zygote" ]; then
        run_args="${run_args} --zygote"
        shift
    elif [ "x$1" = "x--interpreter" ]; then
        run_args="${run_args} --interpreter"
        shift
    elif [ "x$1" = "x--no-verify" ]; then
        run_args="${run_args} --no-verify"
        shift
    elif [ "x$1" = "x--no-optimize" ]; then
        run_args="${run_args} --no-optimize"
        shift
    elif [ "x$1" = "x--no-precise" ]; then
        run_args="${run_args} --no-precise"
        shift
    elif [ "x$1" = "x--invoke-with" ]; then
        shift
        what="$1"
        if [ "x$what" = "x" ]; then
            echo "$0 missing argument to --invoke-with" 1>&2
            usage="yes"
            break
        fi
        run_args="${run_args} --invoke-with ${what}"
        shift
    elif [ "x$1" = "x--dev" ]; then
        run_args="${run_args} --dev"
        dev_mode="yes"
        shift
    elif [ "x$1" = "x--build-only" ]; then
        build_only="yes"
        shift
    elif [ "x$1" = "x--output-path" ]; then
        shift
        tmp_dir=$1
        if [ "x$tmp_dir" = "x" ]; then
            echo "$0 missing argument to --output-path" 1>&2
            usage="yes"
            break
        fi
        shift
    elif [ "x$1" = "x--update" ]; then
        update_mode="yes"
        shift
    elif [ "x$1" = "x--help" ]; then
        usage="yes"
        shift
    elif [ "x$1" = "x--64" ]; then
        run_args="${run_args} --64"
        suffix64="64"
        shift
    elif [ "x$1" = "x--trace" ]; then
        trace="true"
        shift
    elif [ "x$1" = "x--always-clean" ]; then
        always_clean="yes"
        shift
    elif expr "x$1" : "x--" >/dev/null 2>&1; then
        echo "unknown $0 option: $1" 1>&2
        usage="yes"
        break
    else
        break
    fi
done

# tmp_dir may be relative, resolve.
#
# Cannot use realpath, as it does not exist on Mac.
# Cannot us a simple "cd", as the path might not be created yet.
# Cannot use readlink -m, as it does not exist on Mac.
# Fallback to nuclear option:
tmp_dir="`cd $oldwd ; python -c "import os; print os.path.realpath('$tmp_dir')"`"
mkdir -p $tmp_dir

if [ "$trace" = "true" ]; then
    run_args="${run_args} --runtime-option -Xmethod-trace --runtime-option -Xmethod-trace-file:${DEX_LOCATION}/trace.bin --runtime-option -Xmethod-trace-file-size:2000000"
fi

# Most interesting target architecture variables are Makefile variables, not environment variables.
# Try to map the suffix64 flag and what we find in ${ANDROID_PRODUCT_OUT}/data/art-test to an architecture name.
function guess_arch_name() {
    grep32bit=`ls ${ANDROID_PRODUCT_OUT}/data/art-test | grep -E '^(arm|x86|mips)$'`
    grep64bit=`ls ${ANDROID_PRODUCT_OUT}/data/art-test | grep -E '^(arm64|x86_64)$'`
    if [ "x${suffix64}" = "x64" ]; then
        target_arch_name=${grep64bit}
    else
        target_arch_name=${grep32bit}
    fi
}

if [ "$target_mode" = "no" ]; then
    if [ "$runtime" = "jvm" ]; then
        RUN="${progdir}/etc/reference-run-test-classes"
        if [ "$prebuild_mode" = "yes" ]; then
            echo "--prebuild with --jvm is unsupported";
            exit 1;
        fi
    else
        RUN="${progdir}/etc/host-run-test-jar"
        if [ "$prebuild_mode" = "yes" ]; then
            run_args="${run_args} --prebuild"
        fi
    fi
else
    if [ "$prebuild_mode" = "yes" ]; then
        RUN="${progdir}/etc/push-and-run-prebuilt-test-jar"
    fi
fi

if [ ! "$runtime" = "jvm" ]; then
  run_args="${run_args} --lib $lib"
fi

if [ "$runtime" = "dalvik" ]; then
    if [ "$target_mode" = "no" ]; then
        framework="${ANDROID_PRODUCT_OUT}/system/framework"
        bpath="${framework}/core.jar:${framework}/conscrypt.jar:${framework}/okhttp.jar:${framework}/core-junit.jar:${framework}/bouncycastle.jar:${framework}/ext.jar"
        run_args="${run_args} --boot -Xbootclasspath:${bpath}"
    else
        true # defaults to using target BOOTCLASSPATH
    fi
elif [ "$runtime" = "art" ]; then
    if [ "$target_mode" = "no" ]; then
	# ANDROID_BUILD_TOP and ANDROID_HOST_OUT are not set in a build environment.
        if [ -z "$ANDROID_BUILD_TOP" ]; then
	    export ANDROID_BUILD_TOP=$oldwd
        fi
        if [ -z "$ANDROID_HOST_OUT" ]; then
	    export ANDROID_HOST_OUT=$ANDROID_BUILD_TOP/out/host/linux-x86
        fi
        run_args="${run_args} --boot -Ximage:${ANDROID_HOST_OUT}/framework/core.art"
        run_args="${run_args} --runtime-option -Djava.library.path=${ANDROID_HOST_OUT}/lib${suffix64}"
    else
        guess_arch_name
        run_args="${run_args} --runtime-option -Djava.library.path=/data/art-test/${target_arch_name}"
        run_args="${run_args} --boot -Ximage:/data/art-test/core.art"
    fi
    if [ "$relocate" = "yes" ]; then
      run_args="${run_args} --relocate"
    else
      run_args="${run_args} --no-relocate"
    fi
fi

if [ "$dev_mode" = "yes" -a "$update_mode" = "yes" ]; then
    echo "--dev and --update are mutually exclusive" 1>&2
    usage="yes"
fi

if [ "$usage" = "no" ]; then
    if [ "x$1" = "x" -o "x$1" = "x-" ]; then
        test_dir=`basename "$oldwd"`
    else
        test_dir="$1"
    fi

    if [ '!' -d "$test_dir" ]; then
        td2=`echo ${test_dir}-*`
        if [ '!' -d "$td2" ]; then
            echo "${test_dir}: no such test directory" 1>&2
            usage="yes"
        fi
        test_dir="$td2"
    fi
    # Shift to get rid of the test name argument. The rest of the arguments
    # will get passed to the test run.
    shift
fi

if [ "$usage" = "yes" ]; then
    prog=`basename $prog`
    (
        echo "usage:"
        echo "  $prog --help                          Print this message."
        echo "  $prog [options] [test-name]           Run test normally."
        echo "  $prog --dev [options] [test-name]     Development mode" \
             "(dumps to stdout)."
        echo "  $prog --update [options] [test-name]  Update mode" \
             "(replaces expected.txt)."
        echo '  Omitting the test name or specifying "-" will use the' \
             "current directory."
        echo "  Runtime Options:"
        echo "    -O                   Run non-debug rather than debug build (off by default)."
        echo "    -Xcompiler-option    Pass an option to the compiler."
        echo "    --runtime-option     Pass an option to the runtime."
        echo "    --debug              Wait for a debugger to attach."
        echo "    --gdb                Run under gdb; incompatible with some tests."
        echo "    --build-only         Build test files only (off by default)."
        echo "    --interpreter        Enable interpreter only mode (off by default)."
        echo "    --no-verify          Turn off verification (on by default)."
        echo "    --no-optimize        Turn off optimization (on by default)."
        echo "    --no-precise         Turn off precise GC (on by default)."
        echo "    --zygote             Spawn the process from the Zygote." \
             "If used, then the"
        echo "                         other runtime options are ignored."
        echo "    --prebuild           Run dex2oat on the files before starting test. (default)"
        echo "    --no-prebuild        Do not run dex2oat on the files before starting"
        echo "                         the test."
        echo "    --relocate           Force the use of relocating in the test, making"
        echo "                         the image and oat files be relocated to a random"
        echo "                         address before running. (default)"
        echo "    --no-relocate        Force the use of no relocating in the test"
        echo "    --host               Use the host-mode virtual machine."
        echo "    --invoke-with        Pass --invoke-with option to runtime."
        echo "    --dalvik             Use Dalvik (off by default)."
        echo "    --jvm                Use a host-local RI virtual machine."
        echo "    --output-path [path] Location where to store the build" \
             "files."
        echo "    --64                 Run the test in 64-bit mode"
        echo "    --trace              Run with method tracing"
        echo "    --always-clean       Delete the test files even if the test fails."
    ) 1>&2
    exit 1
fi

cd "$test_dir"
test_dir=`pwd`

td_info="${test_dir}/${info}"
td_expected="${test_dir}/${expected}"

if [ ! -r $td_info ]; then
    echo "${test_dir}: missing file $td_info" 1>&2
    exit 1
fi

if [ ! -r $td_expected ]; then
    echo "${test_dir}: missing file $td_expected" 1>&2
    exit 1
fi

# copy the test to a temp dir and run it

echo "${test_dir}: building..." 1>&2

rm -rf "$tmp_dir"
cp -Rp "$test_dir" "$tmp_dir"
cd "$tmp_dir"

if [ '!' -r "$build" ]; then
    cp "${progdir}/etc/default-build" build
fi

if [ '!' -r "$run" ]; then
    cp "${progdir}/etc/default-run" run
fi

if [ '!' -r "$check_cmd" ]; then
    cp "${progdir}/etc/default-check" check
fi

chmod 755 "$build"
chmod 755 "$run"
chmod 755 "$check_cmd"

export TEST_NAME=`basename ${test_dir}`

# To cause tests to fail fast, limit the file sizes created by dx, dex2oat and ART output to 2MB.
file_size_limit=2048
if echo "$test_dir" | grep 089; then
  file_size_limit=5120
elif echo "$test_dir" | grep 083; then
  file_size_limit=5120
elif echo "$test_dir" | grep 115; then
# Native bridge test copies libarttest.so into its directory, which needs 2MB already.
  file_size_limit=5120
fi
if ! ulimit -S "$file_size_limit"; then
   echo "ulimit file size setting failed"
fi

good="no"
if [ "$dev_mode" = "yes" ]; then
    "./${build}" 2>&1
    build_exit="$?"
    echo "build exit status: $build_exit" 1>&2
    if [ "$build_exit" = '0' ]; then
        echo "${test_dir}: running..." 1>&2
        "./${run}" $run_args "$@" 2>&1
        run_exit="$?"
        echo "run exit status: $run_exit" 1>&2
        if [ "$run_exit" = "0" ]; then
            good="yes"
        fi
    fi
elif [ "$update_mode" = "yes" ]; then
    "./${build}" >"$build_output" 2>&1
    build_exit="$?"
    if [ "$build_exit" = '0' ]; then
        echo "${test_dir}: running..." 1>&2
        "./${run}" $run_args "$@" >"$output" 2>&1
        sed -e 's/[[:cntrl:]]$//g' < "$output" >"${td_expected}"
        good="yes"
    else
        cat "$build_output" 1>&2
        echo "build exit status: $build_exit" 1>&2
    fi
elif [ "$build_only" = "yes" ]; then
    good="yes"
    "./${build}" >"$build_output" 2>&1
    build_exit="$?"
    if [ "$build_exit" '!=' '0' ]; then
        cp "$build_output" "$output"
        echo "build exit status: $build_exit" >>"$output"
        diff --strip-trailing-cr -q "$expected" "$output" >/dev/null
        if [ "$?" '!=' "0" ]; then
            good="no"
            echo "BUILD FAILED For ${TEST_NAME}"
        fi
    fi
    # Clean up extraneous files that are not used by tests.
    find $tmp_dir -mindepth 1  ! -regex ".*/\(.*jar\|$output\|$expected\)" | xargs rm -rf
    exit 0
else
    "./${build}" >"$build_output" 2>&1
    build_exit="$?"
    if [ "$build_exit" = '0' ]; then
        echo "${test_dir}: running..." 1>&2
        "./${run}" $run_args "$@" >"$output" 2>&1
    else
        cp "$build_output" "$output"
        echo "build exit status: $build_exit" >>"$output"
    fi
    ./$check_cmd "$expected" "$output"
    if [ "$?" = "0" ]; then
        # output == expected
        good="yes"
        echo "${test_dir}: succeeded!" 1>&2
    fi
fi

(
    if [ "$good" != "yes" -a "$update_mode" != "yes" ]; then
        echo "${test_dir}: FAILED!"
        echo ' '
        echo '#################### info'
        cat "${td_info}" | sed 's/^/# /g'
        echo '#################### diffs'
        diff --strip-trailing-cr -u "$expected" "$output" | tail -n 500
        echo '####################'
        echo ' '
    fi

) 1>&2

# Clean up test files.
if [ "$always_clean" = "yes" -o "$good" = "yes" ]; then
    cd "$oldwd"
    rm -rf "$tmp_dir"
    if [ "$target_mode" = "yes" -a "$build_exit" = "0" ]; then
        adb shell rm -rf $DEX_LOCATION
    fi
    if [ "$good" = "yes" ]; then
        exit 0
    fi
fi


(
    if [ "$always_clean" = "yes" ]; then
        echo "${TEST_NAME} files deleted from host "
        if [ "$target_mode" == "yes" ]; then
            echo "and from target"
        fi
    else
        echo "${TEST_NAME} files left in ${tmp_dir} on host"
        if [ "$target_mode" == "yes" ]; then
            echo "and in ${DEX_LOCATION} on target"
        fi
    fi

) 1>&2

exit 1
